<?php
class Shipping_ShipPriceController extends Zend_Controller_Action
{
	public function indexAction(){

        if($this->_request->isPost()){
            $page = $this->_request->getParam('page',1);
    	    $pageSize = $this->_request->getParam('pageSize',20);
            $area = $this->_request->getParam('area','');
            $st_id = $this->_request->getParam('st_id','');
            $warehouse_id = $this->_request->getParam('warehouse_id','');
			$sp_calc_type = $this->_request->getParam('sp_calc_type','');
            $condition = array(
                'area'            =>$area,
                'st_id'           =>$st_id,
                'warehouse_id'    =>$warehouse_id,
				'sp_calc_type'    =>$sp_calc_type
            );

            $list = Shipping_Service_ShipPrice::getByCondition($condition,'*','sp_id,warehouse_id,st_code,area,sp_weight',$page,$pageSize);
            $list = Shipping_Service_ShipPrice::getInfo($list);
			
            $count = Shipping_Service_ShipPrice::getByCondition($condition,'count(*)');
            $json = array('list'=>$list,'count'=>$count);
            echo Zend_Json::encode($json);
            exit;
        }else{
			$calcTypeList  = Shipping_Service_ShipPrice::getCalcTypeList();
			$shipTypeList  = Shipping_Service_ShipType::getByCondition(array('st_status'=>1),'*','st_code');
            $warehouseList = Warehouse_Service_Warehouse::getByCondition();
            $currencyList  = Common_Service_Currency::getByCondition(array());
			$this->view->shipTypeList  = $shipTypeList;
            $this->view->warehouseList = $warehouseList;
            $this->view->calcTypeList  = $calcTypeList;
            $this->view->currencyList  = $currencyList;
			
            echo Cff::renderTpl("shipping/shipprice/index.tpl","layout");
        }
    }

    public function infoAction(){
        $sp_id = $this->_request->getParam('sp_id',0);
        try{
            $shipPrice = Shipping_Service_ShipPrice::getByValue($sp_id);
            $return = array(
                'ask' =>true,
                'list' =>$shipPrice
            );
        }catch(Exception $e){
            $return = array(
                'ask' =>false,
                'list' =>null
            );
        }
        echo Zend_Json::encode($return);
        exit;
    }


    public function editAction(){
        $lang = Zend_Registry::get('lang');
        $return = array();
        $sp_id = $this->_request->getParam('sp_id',0);
        $sp_weight = $this->_request->getParam('sp_weight',0);
        $sp_cost = $this->_request->getParam('sp_cost',0);
        $area = $this->_request->getParam('area','');
        $sp_calc_type = $this->_request->getParam('sp_calc_type','');
        $st_id = $this->_request->getParam('st_id','');
        $warehouse_id = $this->_request->getParam('warehouse_id','');
        $currency_code = $this->_request->getParam('currency_code','');
        $shipType =Shipping_Service_ShipType::getByValue($st_id);
        $val =array(
			'sp_id' => $sp_id,
            'sp_weight' =>$sp_weight,
            'sp_cost' =>$sp_cost,
            'area' =>$area,

            'sp_calc_type' =>$sp_calc_type,
            'st_id' =>$st_id,
            'warehouse_id' =>$warehouse_id,
            'currency_code' =>$currency_code,
            'st_code' =>@$shipType['st_code'],
        );

        $error = Shipping_Service_ShipPrice::validator($val);
        if(!empty($error)){
            $return['ask'] =false;
            $return['list']['error'] =$error;
            echo Zend_Json::encode($return);
            exit ;
        }
        unset($val['sp_id']);
        if($sp_id>0){
            if(Shipping_Service_ShipPrice::update($val,$sp_id)){
                $return['ask'] =true;
                $return['list']['message'] =$lang['shipping_cost_op'].$lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$lang['shipping_cost_op'].$lang['fail'];
            }
        }else{
            if(Shipping_Service_ShipPrice::add($val)){
                $return['ask'] =true;
                $return['list']['message'][] =$lang['shipping_cost_op'].$lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$lang['shipping_cost_op'].$lang['fail'];
            }
        }
        echo Zend_Json::encode($return);
        exit ;
    }
   
  public function deleteAction(){
        $lang = Zend_Registry::get('lang');
        $return = array();
        $sp_id = $this->_request->getParam('sp_id', 0);
        if (Shipping_Service_ShipPrice::delete($sp_id)) {
            $return['ask'] = true;
            $return['list']['message'] = $lang['shipping_cost_op'] . $lang['success'];
        } else {
            $return['ask'] = false;
            $return['list']['error'][] = $lang['shipping_cost_op'] . $lang['fail'];
        }
        echo Zend_Json::encode($return);
        exit();
    }
    
    public function uploadAction(){    
        $lang = Zend_Registry::get('lang');
    	$type = $this->_request->getParam('type',"ship_price");
    	$ajax = $this->_request->getParam('ajax',"");
    	$this->view->msg = "";
    	$this->view->err = "";
    	$this->view->selectShipType="";
    	$distinctShipType = array();
    	if($this->_request->isPost()){
    		
    		if($type=="ship_type_country_map"){
    			$arr = Shipping_Service_ShipPrice::readUploadFile($_FILES['productFile']['name'],$_FILES['productFile']['tmp_name']);
    			//     		var_dump($arr);exit;
    			if(is_array($arr)){
    				foreach($arr as $k=>$v){
    					if(!empty($v['ship_type'])){
		    				$distinctShipType[$v['ship_type']] = $v['ship_type'];							
						}
    					if(!isset($v['stcm_area'])){
    						$this->view->msg = $lang['upload_file_illegal'];
    						$this->view->err = $lang['upload_file_illegal'];
    						break;
    					}
    					$arr[$k]['cls'] = "ok";
    					$arr[$k]['status'] = 1;
    					foreach($v as $v1){
    						if(empty($v1)){
    							$arr[$k]['cls'] = 'err';
		    					$arr[$k]['status'] = 0;
    							break;
    						}
    					}
    				}
    				
    				$arr_clone = array();
    				foreach($arr as $k=>$v){
    					$key = "";
    					foreach($v as $v1){
    						$key.=$v1."_";
    					}
    					if(isset($arr_clone[$key])){		    				
		    				$arr_clone[$key]['count'] = $arr_clone[$key]['count']+1;	
		    			}else{	
		    				$v['count'] = 1;
		    				$arr_clone[$key] = $v;
		    			}	
    				}
		    		$arr_clone = array_values($arr_clone);
		    		
		    		$arr = $arr_clone;//重新赋值
		    		$this->view->list = $arr;
		    		$this->view->json = Zend_Json::encode($arr);
    			}else{
    				$this->view->msg = $lang['upload_file_illegal'];
    			}
    		}else if($type=="ship_price"){
	    		$arr = Shipping_Service_ShipPrice::readUploadFile($_FILES['productFile']['name'],$_FILES['productFile']['tmp_name']);
	//     		var_dump($arr);exit;
		    	if(is_array($arr)){
					foreach($arr as $k=>$v){
						if(!empty($v['ship_type'])){
		    				$distinctShipType[$v['ship_type']] = $v['ship_type'];							
						}
						if(!isset($v['sp_weight'])){
							$this->view->msg = $lang['upload_file_illegal'];
    						$this->view->err = $lang['upload_file_illegal'];
							break;
						}
		    			$arr[$k]['cls'] = "ok";
    					$arr[$k]['status'] = 1;
		    			foreach($v as $v1){
		    				if(empty($v1)){
		    					$arr[$k]['cls'] = 'err';
		    					$arr[$k]['status'] = 0;
		    					break;    					
		    				}
		    				$arr[$k]['sp_price'] =  sprintf("%.2f",$arr[$k]['sp_price']);
		    				$arr[$k]['sp_weight'] =  sprintf("%.3f",$arr[$k]['sp_weight']);
		    			}
		    			
		    		}
		    		
		    		$arr_clone = array();
		    		foreach($arr as $k=>$v){
    					$key = "";		    			
		    			foreach($v as $v1){		    				
		    				$key.=$v1."_";
		    			}
		    			if(isset($arr_clone[$key])){		    				
		    				$arr_clone[$key]['count'] = $arr_clone[$key]['count']+1;	
		    			}else{	
		    				$v['count'] = 1;
		    				$arr_clone[$key] = $v;
		    			}	    			 
		    		}
		    		$arr_clone = array_values($arr_clone);
// 		    		print_r($arr_clone);exit;
					$arr = $arr_clone;//重新赋值
		    		$this->view->list = $arr;
		    		$this->view->json = Zend_Json::encode($arr);
				}else{
					$this->view->msg = $lang['upload_file_illegal'];
				}
    			
    		}
    		
    		//     		print_r($arr);exit;
    		$value = '1';
    		$field = 'st_status';
    		$shipType = Shipping_Service_ShipType::getAllByValue($value,$field);
    		$shipTypeArr = array();
    		foreach($shipType as $v){
    			$shipTypeArr[$v['st_code']] = $v;
    		}
    		ksort($shipTypeArr);
    		
    		if(count($distinctShipType)>1){
    			$this->view->err =$lang['upload_file_illegal_mult_shipping_method'];
    			$this->view->msg =$lang['upload_file_illegal_mult_shipping_method'];
    		}else if(count($distinctShipType)==1){
    			$distinctShipType = array_keys($distinctShipType);
    			$distinctShipType = $distinctShipType[0];
    			$this->view->selectShipType = $distinctShipType;
    		}
    		$this->view->shipType = $shipTypeArr;
    		
    	}
    	$this->view->op_type = $type;    	
//     	print_r($ajax);exit;
//     	echo $type;
		if($ajax){
			echo Cff::renderTpl("shipping/shipprice/import_ajax.tpl","layout");
		}else{
			echo Cff::renderTpl("shipping/shipprice/import.tpl","layout");
		}
    	
    }
    
    public function batchAddAction(){  
    	set_time_limit (0);
    	$row = $this->_request->getParam("row");    	
    	$shipType = $this->_request->getParam("ship_type");
    	$warehouseId = $this->_request->getParam("warehouse_id",1);
    	$op_type = $this->_request->getParam("op_type","ship_price");
    	$this->view->msg = "";
//     	print_r($shipType);exit;
    	if($op_type=="ship_price"){    		
    		$result = Shipping_Service_ShipPrice::batchAddShipPrice($shipType, $row,$warehouseId);    		
    	}else if($op_type=="ship_type_country_map"){
    		$result = Shipping_Service_ShipTypeCountryMap::batchAddShipTypeCountryMap($shipType, $row);                                                             
    	}
    	if($result['status']==1){
    		$this->view->errlist = $result['errlist'];    		
    	}
    	
    	$this->view->msg = $result['msg'];
    	$this->view->op_type = $op_type;
    	echo Cff::renderTpl("shipping/shipprice/import.tpl","layout");
    }
}